package cn.chinawyny.boot.bootdemo.controller;

import cn.chinawyny.boot.bootdemo.pojo.dto.KafkaMessageDto;
import cn.chinawyny.boot.bootdemo.properties.MyProperties;
import cn.chinawyny.boot.bootdemo.services.MyService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

@Controller
//@ConditionalOnExpression("myService.isAccess()")
//@ConditionalOnProperty(name = "server.port",havingValue = "6033")
//@ConditionalOnResource(resources = "file://D://1.txt")
public class HelloWorldController {


    @Resource
    private MyProperties myProperties;

    @Resource
    private KafkaTemplate<String,byte[]> kafkaTemplate;

    @Resource
    private MyService myService;

    @GetMapping("/hello/{id}")
    public String hello(Model model, @PathVariable String id) {
        String s = myService.printHello();
        model.addAttribute("a", s);
        ClassPathResource classPathResource = new ClassPathResource("/");
        System.out.println(classPathResource.getPath());
        return "hello/index";
    }

    @PostMapping("/send/kafka")
    @ResponseBody
    public void hello(@Validated @RequestBody KafkaMessageDto kafkaMessageDto) {
        kafkaTemplate.send(kafkaMessageDto.getTopic(),kafkaMessageDto.getMessage().getBytes(StandardCharsets.UTF_8));
    }
}
